﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace KalevaAalto.DataPart.Excel.Models
{
    public class RangePos
    {

        private CellPos _startPos;
        private CellPos _endPos;
        public RangePos(CellPos startPos, CellPos endPos)
        {
            if (endPos.Row < startPos.Row || endPos.Column < startPos.Column)
            {
                throw new Exception(@"单元格区域结束点应在开始点之后；");
            }
            _startPos = startPos;
            _endPos = endPos;
        }


        public static readonly Regex AddressRegex = new Regex(@"(?<startPos>[a-zA-Z]+\d+)\:(?<endPos>[a-zA-Z]+\d+)",RegexOptions.Compiled);
        public RangePos(string address)
        {
            Match match = AddressRegex.Match(address);
            if (match.Success)
            {
                CellPos startPos = new CellPos(match.Groups[@"startPos"].Value);
                CellPos endPos = new CellPos(match.Groups[@"endPos"].Value);
                if (endPos.Row < startPos.Column || endPos.Column < startPos.Column)
                {
                    throw new Exception(@"单元格区域结束点应在开始点之后；");
                }
                _startPos = startPos;
                _endPos = endPos;
            }
            else throw new Exception($"“{address}”不是合法的单元格地址；");

        }
        public RangePos(int startPosRow, int startPosColumn, int endPosRow, int endPosColumn)
        {
            if (endPosRow < startPosRow || endPosColumn < startPosColumn)
            {
                throw new Exception(@"单元格区域结束点应在开始点之后；");
            }
            _startPos = new CellPos(startPosRow, startPosColumn);
            _endPos = new CellPos(endPosRow, endPosColumn);
        }



        public CellPos StartPos { get => _startPos; set => _startPos = value; }
        public CellPos EndPos { get => _endPos; set => _endPos = value; }
        public string Address { get => $"{StartPos.Address}:{EndPos.Address}"; }


        public override string ToString() => Address;

        public override bool Equals(object? obj)
        {
            if (obj is RangePos other)
            {
                return _startPos.Equals(other._startPos)
                    && _endPos.Equals(other._endPos);
            }
            else return false;
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int hash = 17;
                hash = hash * 23 + _startPos.GetHashCode();
                hash = hash * 23 + _endPos.GetHashCode();
                return hash;
            }
        }

    }
}
